<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
/**
 * This class is meant to be a slightly more sofisticated object than an array
 * It is used to return data from an Adapter and to be the universal way of
 * exchanging data on the most basic level.
 *
 */
class X_Data_Dataset extends X_Abstract_Object implements Iterator, X_Interface_DataSource, X_Interface_Serializable, X_Interface_Consumer
{
    protected $_sPrimaryKey;
    
    function __construct (Array $aAttributes = array())
    {
        $this->setAttributes($aAttributes);
    }
    
    public function rewind ()
    {
        reset($this->_aAttributes);
    }
    
    public function current ()
    {
        return current($this->_aAttributes);
    }
    
    public function key ()
    {
        return key($this->_aAttributes);
    }
    
    public function next ()
    {
        return next($this->_aAttributes);
    }
    
    public function valid ()
    {
        return current($this->_aAttributes) || false;
    }
    /**
     * Get data from a data source
     *
     * @return array (of associative arrays)
     */
    public function getData ()
    {
        reset($this->_aAttributes);
        // this may be 2D structured already
        if (is_array(current($this->_aAttributes)))
        {
            return $this->_aAttributes;
        }
        
        // make sure we return 2D array
        return array(
            
            $this->_aAttributes
        );
    }
    
    /**
     * set the column for use as the array index when consuming
     *
     * @param string $sIndex
     * @return $this
     */
    public function setPrimaryKey ($sIndex)
    {
        $this->_sPrimaryKey = $sIndex;
        return $this;
    }
    
    public function getPrimaryKey ()
    {
        return $this->_sPrimaryKey;
    }
    
    /**
     * 	process/consume the given array
     *
     * 	@return void
     */
    public function consume (Array $aData)
    {
        if (empty($this->_sPrimaryKey) || ! array_key_exists($this->_sPrimaryKey, $aData))
        {
            $this->_aAttributes[] = $aData;
        }
        else
        {
            $this->_aAttributes[$aData[$this->_sPrimaryKey]] = $aData;
        }
    }
    
    /**
     * fill the consumer object with data
     *
     * @param X_Interface_Consumer $oConsumer
     * @return void
     */
    public function fill (X_Interface_Consumer $oConsumer)
    {
        foreach ($this->_aAttributes as $aRow)
        {
            $oConsumer->consume($aRow);
        }
    
    }
    
    /**
     * 	What properties are we going to save for this object?
     * just the attributes/data
     *
     * 	@return array
     */
    public function __sleep ()
    {
        return array(
            '_aAttributes'
        );
    }
    /**
     * 	How do we want to rebuild our object on unserialization?
     * we dont need to do anything
     *
     */
    public function __wakeup ()
    {}
}
